﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_BCFAInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel ShowPro = (Panel)FindControl("ShowPro");
            if (!base.Page.IsPostBack)
            {
                string id = _EluZSDataProvider.GetBCFAID(HttpContext.Current.Request["ifafter"], HttpContext.Current.Request["PID"]);
                Persistence1.Table = "ZS_BCFAQS";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = id;
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();                              
            }
            Persistence1.BindControl = ShowPro;
            BindData();
            if (string.IsNullOrEmpty(Persistence1.Key))
            {
                Button btnUpload = (Button)FindControl("btnUpload");
                btnUpload.Attributes.Add("style", "display:none");
            }
            string strPID = base.Page.Request["PID"];
            if (_EluZSDataProvider.GetProjectFJNo(strPID, "土地权属证明文件", "1") != 0)
            {
                Antu.Utilities.UtilitiesPage.RegJsScript("displaytr();");
            }
            IsLookPerson();
        }


        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnSave = (Button)FindControl("btnSave");
                        btnSave.Visible = false;
                    }
                }
            }
        }

        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Panel ShowPro = (Panel)FindControl("ShowPro");
            string pid = HttpContext.Current.Request["PID"].ToString();
            string tt = HttpContext.Current.Request["ifafter"];
            TextBox txtPID = (TextBox)FindControl("txtPID");
            TextBox txt补偿实施 = (TextBox)FindControl("txt补偿实施");
            txtPID.Text = pid;
            txt补偿实施.Text = tt;
            EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(pid);
            TextBox txt征收范围 = (TextBox)ShowPro.FindControl("txt征收范围");
            TextBox txt房屋建筑面积 = (TextBox)ShowPro.FindControl("txt房屋建筑面积");
            TextBox txt权证户数 = (TextBox)ShowPro.FindControl("txt权证户数");
            TextBox txt房屋建筑面积住宅 = (TextBox)ShowPro.FindControl("txt房屋建筑面积住宅");
            TextBox txt户口户数 = (TextBox)ShowPro.FindControl("txt户口户数");
            TextBox txt房屋建筑面积非住宅 = (TextBox)ShowPro.FindControl("txt房屋建筑面积非住宅");
            TextBox txt工企单位 = (TextBox)ShowPro.FindControl("txt工企单位");
            TextBox txt征收实施单位 = (TextBox)ShowPro.FindControl("txt征收实施单位");
            if (string.IsNullOrEmpty(txt征收范围.Text.Trim())) { txt征收范围.Text = project.征收范围; }
            if (string.IsNullOrEmpty(txt房屋建筑面积.Text.Trim())) { txt房屋建筑面积.Text = project.被征收房屋面积合计; }
            if (string.IsNullOrEmpty(txt权证户数.Text.Trim())) { txt权证户数.Text = project.被征收居民权证户数; }
            if (string.IsNullOrEmpty(txt房屋建筑面积住宅.Text.Trim())) { txt房屋建筑面积住宅.Text = project.被征收房屋面积住宅; }
            if (string.IsNullOrEmpty(txt户口户数.Text.Trim())) { txt户口户数.Text = project.被征收居民户口户数; }
            if (string.IsNullOrEmpty(txt房屋建筑面积非住宅.Text.Trim())) { txt房屋建筑面积非住宅.Text = project.被征收房屋面积非住宅; }
            if (string.IsNullOrEmpty(txt工企单位.Text.Trim())) { txt工企单位.Text = project.被征收工企单位; }
            if (string.IsNullOrEmpty(txt征收实施单位.Text.Trim())) { txt征收实施单位.Text = project.征收实施单位; }
            TextBox 补偿提交状态 = (TextBox)FindControl("补偿提交状态");
            
            if (!string.IsNullOrEmpty(tt)) {
                if (tt == "0") { 补偿提交状态.Text = project.补偿提交状态; }
                else if (tt == "1") { 
                    补偿提交状态.Text = project.补偿实施提交状态;
                    EluZSProvider.BCFAQS bcfaqs = _EluZSDataProvider.GetBCFAQS("0", pid);
                    TextBox txt房屋征收目的 = (TextBox)FindControl("txt房屋征收目的");
                    TextBox txt土地使用面积 = (TextBox)FindControl("txt土地使用面积");
                    TextBox txt产权调换房源地点 = (TextBox)FindControl("txt产权调换房源地点");
                    TextBox txt保障房源地点 = (TextBox)FindControl("txt保障房源地点");
                    TextBox txt产权调换房源数量 = (TextBox)FindControl("txt产权调换房源数量");
                    TextBox txt保障房源数量 = (TextBox)FindControl("txt保障房源数量");
                    TextBox txt征收补偿资金 = (TextBox)FindControl("txt征收补偿资金");
                    TextBox txt征收签约期限 = (TextBox)FindControl("txt征收签约期限");
                    if (string.IsNullOrEmpty(txt房屋征收目的.Text.Trim())) { txt房屋征收目的.Text = bcfaqs.房屋征收目的; }
                    if (string.IsNullOrEmpty(txt土地使用面积.Text.Trim())) { txt土地使用面积.Text = bcfaqs.土地使用面积; }
                    if (string.IsNullOrEmpty(txt产权调换房源地点.Text.Trim())) { txt产权调换房源地点.Text = bcfaqs.产权调换房源地点; }
                    if (string.IsNullOrEmpty(txt保障房源地点.Text.Trim())) { txt保障房源地点.Text = bcfaqs.保障房源地点; }
                    if (string.IsNullOrEmpty(txt产权调换房源数量.Text.Trim())) { txt产权调换房源数量.Text = bcfaqs.产权调换房源数量; }
                    if (string.IsNullOrEmpty(txt保障房源数量.Text.Trim())) { txt保障房源数量.Text = bcfaqs.保障房源数量; }
                    if (string.IsNullOrEmpty(txt征收补偿资金.Text.Trim())) { txt征收补偿资金.Text = bcfaqs.征收补偿资金; }
                    if (string.IsNullOrEmpty(txt征收签约期限.Text.Trim())) { txt征收签约期限.Text = bcfaqs.征收签约期限; }
                    if (string.IsNullOrEmpty(txt征收范围.Text.Trim())) { txt征收范围.Text = bcfaqs.征收范围; }
                    if (string.IsNullOrEmpty(txt房屋建筑面积.Text.Trim())) { txt房屋建筑面积.Text = bcfaqs.房屋建筑面积; }
                    if (string.IsNullOrEmpty(txt权证户数.Text.Trim())) { txt权证户数.Text = bcfaqs.权证户数; }
                    if (string.IsNullOrEmpty(txt房屋建筑面积住宅.Text.Trim())) { txt房屋建筑面积住宅.Text = bcfaqs.房屋建筑面积住宅; }
                    if (string.IsNullOrEmpty(txt户口户数.Text.Trim())) { txt户口户数.Text = bcfaqs.户口户数; }
                    if (string.IsNullOrEmpty(txt房屋建筑面积非住宅.Text.Trim())) { txt房屋建筑面积非住宅.Text = bcfaqs.房屋建筑面积非住宅; }
                    if (string.IsNullOrEmpty(txt工企单位.Text.Trim())) { txt工企单位.Text = bcfaqs.工企单位; }
                    if (string.IsNullOrEmpty(txt征收实施单位.Text.Trim())) { txt征收实施单位.Text = bcfaqs.征收实施单位; }
                }
            }

            string strPID = base.Page.Request["PID"];
            if (_EluZSDataProvider.GetProjectFJNo(strPID, "土地权属证明文件", "1") != 0)
            {
                Antu.Utilities.UtilitiesPage.RegJsScript("displaytr();");
            }
        }
        protected void BindData() {
            string pid = HttpContext.Current.Request["pid"];

            Button btnSave = (Button)FindControl("btnSave");
            Button btnReturn = (Button)FindControl("btnReturn");
            Button btnUpload = (Button)FindControl("btnUpload");
            Button btnUpload1 = (Button)FindControl("btnUpload1");
            LinkButton LinkButton1 = (LinkButton)FindControl("LinkButton1");
            Label Label1 = (Label)FindControl("Label1");
            Label Label2 = (Label)FindControl("Label2");
            btnSave.Click += new EventHandler(btnSave_Click);
            btnReturn.Click+=new EventHandler(btnReturn_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
            btnUpload1.Click += new EventHandler(btnUpload1_Click);

            LinkButton1.Click += new EventHandler(LinkButton1_Click);

            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                LinkButton1.Visible = false;
            }
            if (HttpContext.Current.Request["ifafter"] == "1")
            {
                Label1.Text = "征收补偿实施方案申请表"; Label2.Text = "征收补偿实施方案申请表";
                //if (_EluZSDataProvider.GetProjectFJNo(HttpContext.Current.Request["PID"], "土地权属证明文件", "") == 0)
                //{
                //    Page.ClientScript.RegisterStartupScript(this.GetType(), "displaytr", "<script>displaytr();</script>");
                //}
            }
            else if (HttpContext.Current.Request["ifafter"] == "0") {
                Label1.Text = "征收补偿方案申请表"; Label2.Text = "征收补偿方案申请表";
            }
        }
        protected void Bind() {
            TextBox txt冻结征收范围 = (TextBox)FindControl("txt冻结征收范围");
            TextBox txt申请单位 = (TextBox)FindControl("txt申请单位");
            EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(HttpContext.Current.Request["PID"].ToString());
            txt冻结征收范围.Text = project.征收范围;
            txt申请单位.Text = project.征收部门;
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string temp = HttpContext.Current.Request["ifafter"];
            if (temp == "0")
            {
                HttpContext.Current.Response.Redirect("ZhengSFWLog.aspx?type=2&pid=" + HttpContext.Current.Request["PID"] + "&zt=" + HttpContext.Current.Request["zt"]);
            }
            else if (temp == "1") {
                HttpContext.Current.Response.Redirect("ZhengSFWLog.aspx?type=3&pid=" + HttpContext.Current.Request["PID"] + "&zt=" + HttpContext.Current.Request["zt"]);
            }
            
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {            
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            //HttpContext.Current.Response.Redirect("ZS_BCFAInfo.aspx?PID=" + HttpContext.Current.Request["PID"] + "&ifafter=" + HttpContext.Current.Request["ifafter"] + "&zt=" + HttpContext.Current.Request["zt"]);
            string url = "ZS_BCFAInfo.aspx?PID=" + HttpContext.Current.Request["PID"] + "&ifafter=" + HttpContext.Current.Request["ifafter"] + "&zt=" + HttpContext.Current.Request["zt"];
            base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功');location.href='" + url + "'</script>");
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string temp = HttpContext.Current.Request["ifafter"];
            if (temp == "0")
            {
                HttpContext.Current.Response.Redirect("ZS_BCFA.aspx");
            }
            else
            {
                HttpContext.Current.Response.Redirect("ZS_BCSJFA.aspx");
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            TextBox 补偿提交状态 = (TextBox)FindControl("补偿提交状态");
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            string strType = HttpContext.Current.Request["ifafter"];
            string strScript = string.Empty;
            //设置文件上传的关键字段
            if (!string.IsNullOrEmpty(Persistence1.KeyValue))
            {
                if (strType == "0")
                {
                    if (补偿提交状态.Text.Trim()=="1")
                    {
                        strScript = string.Format("<script>OpenFileUpload1('{0}','{1}','{2}','{3}');</script>",
                   Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"show");
                    }
                    else
                    {
                        strScript = string.Format("<script>OpenFileUpload1('{0}','{1}','{2}','{3}');</script>",
                   Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"");
                    }
                   
                }
                else if (strType == "1")
                {
                    if (补偿提交状态.Text.Trim() == "1")
                    {
                        strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}','{4}');</script>", "7",
                        Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"show");
                    }
                    else
                    {
                        strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}','{3}','{4}');</script>", "7",
                        Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(), "");
                    }
                }
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }
        protected void btnUpload1_Click(object sender, EventArgs e)
        {
            TextBox 补偿提交状态 = (TextBox)FindControl("补偿提交状态");
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["ifafter"]))
            {
                if (HttpContext.Current.Request["ifafter"] == "1")
                {
                    if (补偿提交状态.Text.Trim() == "1")
                    {
                        string strScript = string.Format("<script>OpenFileUpload2('{0}','{1}','{2}','{3}');</script>",
                        Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"show");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
                    }
                    else
                    {
                        string strScript = string.Format("<script>OpenFileUpload2('{0}','{1}','{2}','{3}');</script>",
                       Persistence1.KeyValue, HttpContext.Current.Request["PID"].Trim(), HttpContext.Current.Request["PID"].Trim(),"");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
                    }
                }
            }
        }
    }
}


